package br.com.fox.iebav.session.abstractsession;

import java.io.Serializable;
import java.text.Normalizer;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import br.com.fox.iebav.controller.abstractcommand.InterfaceCommand;

public abstract class AbstractSession {
		
	protected boolean redirecionarURL(String url)
	{
		try
		{
			FacesContext.getCurrentInstance().getExternalContext().
			redirect(url);
			return true;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}	
	}
	
	protected Map<String,Object> executarComando(InterfaceCommand ic, Serializable s)
	{
		Map<String, Object>	hm = ic.execute(s);
		return hm;
	}
	
	protected String normalizarString(String txt)
	{
		if(txt==null)
			return "";
		
		txt = Normalizer.normalize(txt,Normalizer.Form.NFD);
	    
		txt = txt.replaceAll("[^\\p{ASCII}]", "");
		return txt;
	}
	
	protected boolean validarEmail(String email)
	{
		  //Set the email pattern string  
		Pattern p = Pattern.compile(".+@.+\\.[a-z]+");  
	      //Match the given string with the pattern  
	    Matcher m = p.matcher(email);  
	      //check whether match is found   
	      boolean matchFound = m.matches();  
	  
	      return matchFound;
	}
	
	
	public String horaAtualFormatada(GregorianCalendar gc)
	{
		 int hora 	= gc.get(Calendar.HOUR_OF_DAY);
	   	 int min 	= gc.get(Calendar.MINUTE);
	   	 int sec 	= gc.get(Calendar.SECOND);
	   	 
	 
   	 String horaDia = 
   		 "( "+String.format("%02d", hora)+":"+String.format("%02d", min)+":"+String.format("%02d", sec)+" Hs )";
   	
   	 return horaDia;
   	}
	
		
	public String converterDoubleMoedaBR(Double valor)
	{
		return NumberFormat.getCurrencyInstance(new Locale("pt","BR")).format(valor);
	}
	
	public String converterDoubleMoedaString(Double valor)
	{
		String valorString = String.format("%.2f", valor);
		
		return valorString;
	}
	
	public void facesMensagens(FacesMessage.Severity msg,String title,String desc){
		
		FacesMessage msg2 = new FacesMessage();
		msg2.setSeverity(msg);
		msg2.setSummary(title);
		msg2.setDetail(desc);
		FacesContext.getCurrentInstance().addMessage(null, msg2);
	}
	
	public boolean verificaTextoLetrasNumEspacoHifenUnder(String texto){
		char letter = texto.charAt(0);
		
		for( int x=0; x < texto.length();x++ ) {
			letter = texto.charAt(x);
			if( (letter < 97) || (letter > 122)) // se nao for letra minuscula
				if( (letter < 65) || (letter > 90) ) // se nao for letra maiuscula
					if( (letter < 48) || (letter > 57) ) // se nao for numero
						if( (letter != ' ') && (letter != '_') && (letter != '-') ) // se nao for espaco e nao for underline
							return false;
		}
		return true;
	}

}
